package com.aos.erp.schools.service.division.impl;

import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.common.constant.GlobalConstant;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.common.utils.ValidationUtil;
import com.aos.erp.schools.model.division.entity.Division;
import com.aos.erp.schools.mapper.division.DivisionMapper;
import com.aos.erp.schools.model.division.ro.AddDivisionRo;
import com.aos.erp.schools.model.division.ro.InfoDivisionRo;
import com.aos.erp.schools.model.division.ro.QueryDivisionRo;
import com.aos.erp.schools.model.division.ro.UpdateDivisionRo;
import com.aos.erp.schools.model.division.vo.ShowDivisionVo;
import com.aos.erp.schools.model.gatherschool.entity.GatherSchool;
import com.aos.erp.schools.service.division.DivisionService;
import com.aos.erp.schools.service.sys.DepartmentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 省市对应分所 服务实现类
 * </p>
 *
 * @author chb
 * @since 2018-10-31
 */
@Service
public class DivisionServiceImpl extends ServiceImpl<DivisionMapper, Division> implements DivisionService {

	@Resource
	private DivisionMapper divisionMapper;

	@Resource
	private DepartmentService departmentService;

	@Resource
	GlobalConstant globalConstant;

	@Override
	public ResultEntity addBean(List<AddDivisionRo> division,String currentUserId) throws Exception {
		Date date=new Date();
		//循环车管所
		for (AddDivisionRo addDivisionRo: division){
			//获取组织code
			String depCode=departmentService.getDepCodeDivision(addDivisionRo.getCode());
			Division divisionInfo=new Division();
			//转换po
			BeanUtils.copyProperties(addDivisionRo,divisionInfo);
			divisionInfo.setCreateUser(Long.parseLong(currentUserId));
			//验证对象
			ValidationUtil.validate(divisionInfo);
			Condition condition = new Condition(Division.class);
			condition.eqColumn("name", divisionInfo.getName());
			condition.eqColumn("city_id",divisionInfo.getCityId());
			//查询是否存在分所
			List<Division> selectList=baseMapper.selectList(condition.getWrapper());
			if(selectList.size()>0){
				return ResultEntity.failed(ResultEntity.CODE_FAIL,"已有分所，请勿重复添加");
			}
			//默认不删除
			divisionInfo.setDepCode(depCode);
			divisionInfo.setIsDel(0);
			divisionInfo.setUpdated(date);
			divisionInfo.setCreated(date);
			boolean flag=this.save(divisionInfo);
			////////////////////////////////////////////
			//添加组织
			if(flag){
				departmentService.addBeanDivision(depCode,addDivisionRo.getName(),globalConstant.getAPP_CODE_SCHOOL());
			}
			////////////////////////////////////////////
		}
		return ResultEntity.success();
	}

	@Override
	public ResultEntity deleteDivision(String id) {
		//根据id查询是否存在分所数据
		Division division = baseMapper.selectById(id);
		if (division == null || division.getIsDel() == -1) {
			return ResultEntity.failed("该车管所不存在");
		}
		//设置为逻辑删除
		division.setIsDel(-1);
		int count =baseMapper.updateById(division);
		if (count!=1) {
			return ResultEntity.failed("删除失败");
		}
		return ResultEntity.success();
	}

	@Override
	public ResultEntity updateDivision(List<UpdateDivisionRo> addDivisionRoList) throws BizException {
		Date date=new Date();
		for (UpdateDivisionRo updateDivisionRo: addDivisionRoList ) {
			if(updateDivisionRo.getId()==null){
			//如果有新增车管所，则新增
				Condition condition = new Condition(Division.class);
				condition.eqColumn("name", updateDivisionRo.getName());
				condition.eqColumn("city_id",updateDivisionRo.getCityId());
				//查询是否存在分所
				List<Division> selectList=baseMapper.selectList(condition.getWrapper());
				if(selectList.size()>0){
					return ResultEntity.failed(ResultEntity.CODE_FAIL,"已有车管所，请勿重复添加");
				}
				Division division=new Division();
				//获取组织code
				String depCode=departmentService.getDepCodeDivision(updateDivisionRo.getCode());
				division.setCreateUser(updateDivisionRo.getCurrentUserId());
				division.setCityId(updateDivisionRo.getCityId());
				division.setProvinceId(updateDivisionRo.getProvinceId());
				division.setCode(updateDivisionRo.getCode());
				division.setName(updateDivisionRo.getName());
				division.setIsDel(0);
				division.setDepCode(depCode);
				division.setCreated(date);
				division.setUpdated(date);
				baseMapper.insert(division);
				departmentService.addBeanDivision(depCode,updateDivisionRo.getName(),globalConstant.getAPP_CODE_SCHOOL());
			}else{
				baseMapper.updateDivision(updateDivisionRo);
			}
		}
		return ResultEntity.success();
	}

	@Override
	public List<InfoDivisionRo> divisionInfo(Long cityId) {
		Condition condition = new Condition(Division.class);
		condition.eqColumn("city_id",cityId);
		condition.eqColumn("is_del",0);
		List<InfoDivisionRo> infoDivisionRo=new ArrayList<InfoDivisionRo>();
		List<Division> divisions=baseMapper.selectList(condition.getWrapper());
		for (Division division:divisions) {
			InfoDivisionRo info=new InfoDivisionRo();
			//转换po
			BeanUtils.copyProperties(division,info);
			infoDivisionRo.add(info);
		}
		return infoDivisionRo;
	}

	@Override
	public IPageBean findByPage( QueryDivisionRo queryDivisionRo) {

		Map<String,Object> map=new HashMap<String, Object>();
		map.put("provinceId",queryDivisionRo.getProvinceId());
		map.put("cityId",queryDivisionRo.getCityId());
		IPage<ShowDivisionVo> page = new Page<ShowDivisionVo>(queryDivisionRo.getPageIndex()==null?1:queryDivisionRo.getPageIndex(),queryDivisionRo.getPageSize()==null?10:queryDivisionRo.getPageSize());
		map.put("IPage",page);
		List<ShowDivisionVo> list=divisionMapper.queryListVo(map);
		page.setRecords(list);

		return baseMapper.toBizPage(page);
	}

	@Override
	public  Division info(Long id) {
		return this.baseMapper.selectById(id);
	}

	@Override
	public Division infoByDepCode(String depCode) {
		Division division=divisionMapper.infoByDepCode(depCode);
		return division;
	}

}
